package com.jxj.controller;

import com.jxj.dao.AddrTimeMapper;
import com.jxj.dao.OrgzMapper;
import com.jxj.dao.RecruitMapper;
import com.jxj.dao.RecruitMsgMapper;
import com.jxj.entity.AddrTime;
import com.jxj.entity.RecruitMsg;
import com.jxj.entity.Resp;
import com.jxj.entity.gen.TAddrtime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class RecruitMsgController {
    @Resource
    private OrgzMapper orgzMapper;
    @Resource
    private RecruitMsgMapper recruitMsgMapper;
    @Resource
    private AddrTimeMapper addrTimeMapper;
    static String ip = "https://hd.jxj777.top";

    @PostMapping("/organization/post")
    public Resp addRecruitMsg(RecruitMsg recruitMsg,
                              @RequestParam("organization") String organization,
                              @RequestParam("QRcode") MultipartFile QRcode,
                              @RequestParam("video") MultipartFile video,
                              @RequestParam("timeaddr") AddrTime[] addrTimes
    ) throws IOException {

        int oId = orgzMapper.selectOId(organization);
        recruitMsg.setOId(oId);


        if (!QRcode.isEmpty() && !video.isEmpty() && addrTimes != null) {
            String videoOriginalFilename = video.getOriginalFilename();
            String QRcodeOriginalFilename = QRcode.getOriginalFilename();
            String classespath = "/usr/img/";
            video.transferTo(new File(classespath + videoOriginalFilename));
            QRcode.transferTo(new File(classespath + QRcodeOriginalFilename));
            String filePath = ip + "/usr/img/";
            recruitMsg.setQrCode_path(filePath + QRcodeOriginalFilename);
            recruitMsg.setVideo_path(filePath + videoOriginalFilename);

        } else {
            return new Resp(HttpStatus.BAD_REQUEST, "缺少视频或二维码或时间地点", null);
        }

        int flag = recruitMsgMapper.addRecruit(recruitMsg);
        if (flag > 0) {

            int rId = recruitMsgMapper.selectRId();

            for (AddrTime addrTime : addrTimes) {
                addrTime.setRId(rId);
                addrTimeMapper.addAddrTime(addrTime);
            }
        }
        log.info(recruitMsg.toString());

        return null;
    }
}
